using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VCUBST
    {
        public DataRow Convert2CUBST(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryCUBST(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["CUBMANDANT"] = " ".PadRight(2 ); 
         dwReturn["CUBRWKRS"] = " ".PadRight(2 ); 
         dwReturn["CUBRWLFD"] = " ".PadRight(6 ); 
         dwReturn["CUBRWPOS"] = " ".PadRight(4 ); 
         dwReturn["CUBZUSTKRS"] = " ".PadRight(2 ); 
         dwReturn["CUBZUSTLFD"] = " ".PadRight(4 ); 
         dwReturn["CUBZUSTPOS"] = " ".PadRight(2 ); 
         dwReturn["CUBLIEFDAT"] = " ".PadRight(0 ); 
         dwReturn["CUBAEDAT"] = " ".PadRight(0 ); 
         dwReturn["CUBFOLGE"] = " ".PadRight(0 ); 
         dwReturn["CUBLO"] = " ".PadRight(2 ); 
         dwReturn["CUBLB"] = " ".PadRight(2 ); 
         dwReturn["CUBLP"] = " ".PadRight(8 ); 
         dwReturn["CUBAUFBKZ"] = " ".PadRight(1 ); 
         dwReturn["CUBSTKNR"] = " ".PadRight(12 ); 
         dwReturn["CUBANZCUT"] = " ".PadRight(0 ); 
         dwReturn["CUBSTATUS"] = " ".PadRight(2 ); 
         dwReturn["CUBSTPLNR"] = " ".PadRight(3 ); 
         dwReturn["CUBSTPLGRP"] = " ".PadRight(2 ); 
         dwReturn["CUBFAZEIT"] = " ".PadRight(0 ); 
         dwReturn["CUBART"] = " ".PadRight(20 ); 
         dwReturn["CUBSPERRE"] = " ".PadRight(1 ); 
         dwReturn["CUBNEUB"] = " ".PadRight(8 ); 
         dwReturn["CUBNEUD"] = " ".PadRight(0 ); 
         dwReturn["CUBAENB"] = " ".PadRight(8 ); 
         dwReturn["CUBAEND"] = " ".PadRight(0 ); 
         dwReturn["CUBAENZL"] = " ".PadRight(0 ); 
         dwReturn["CUBFILLER"] = " ".PadRight(85 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapCUBST(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveCUBST(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryCUBST(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapCUBST(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertCUBST(dwLocal, ITXDBVERSION);
            }
            UpdateCUBST(dwLocal);
        }
        public void InsertCUBST(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO CUBST(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryCUBST(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM CUBST WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM CUBST  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "CUBST";
            Return = Result;
            return Return;
        }
        public void DeleteCUBST(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM CUBST   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateCUBST(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE CUBST SET "
                    +"CUBMANDANT= :CUBMANDANT,"
                    +"CUBRWKRS= :CUBRWKRS,"
                    +"CUBRWLFD= :CUBRWLFD,"
                    +"CUBRWPOS= :CUBRWPOS,"
                    +"CUBZUSTKRS= :CUBZUSTKRS,"
                    +"CUBZUSTLFD= :CUBZUSTLFD,"
                    +"CUBZUSTPOS= :CUBZUSTPOS,"
                    +"CUBLIEFDAT= :CUBLIEFDAT,"
                    +"CUBAEDAT= :CUBAEDAT,"
                    +"CUBFOLGE= :CUBFOLGE,"
                    +"CUBLO= :CUBLO,"
                    +"CUBLB= :CUBLB,"
                    +"CUBLP= :CUBLP,"
                    +"CUBAUFBKZ= :CUBAUFBKZ,"
                    +"CUBSTKNR= :CUBSTKNR,"
                    +"CUBANZCUT= :CUBANZCUT,"
                    +"CUBSTATUS= :CUBSTATUS,"
                    +"CUBSTPLNR= :CUBSTPLNR,"
                    +"CUBSTPLGRP= :CUBSTPLGRP,"
                    +"CUBFAZEIT= :CUBFAZEIT,"
                    +"CUBART= :CUBART,"
                    +"CUBSPERRE= :CUBSPERRE,"
                    +"CUBNEUB= :CUBNEUB,"
                    +"CUBNEUD= :CUBNEUD,"
                    +"CUBAENB= :CUBAENB,"
                    +"CUBAEND= :CUBAEND,"
                    +"CUBAENZL= :CUBAENZL,"
                    +"CUBFILLER= :CUBFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("CUBMANDANT", OracleType.NVarChar).Value = dwData["CUBMANDANT"].ToString(); 
          or1.Parameters.Add("CUBRWKRS", OracleType.NVarChar).Value = dwData["CUBRWKRS"].ToString(); 
          or1.Parameters.Add("CUBRWLFD", OracleType.NVarChar).Value = dwData["CUBRWLFD"].ToString(); 
          or1.Parameters.Add("CUBRWPOS", OracleType.NVarChar).Value = dwData["CUBRWPOS"].ToString(); 
          or1.Parameters.Add("CUBZUSTKRS", OracleType.NVarChar).Value = dwData["CUBZUSTKRS"].ToString(); 
          or1.Parameters.Add("CUBZUSTLFD", OracleType.NVarChar).Value = dwData["CUBZUSTLFD"].ToString(); 
          or1.Parameters.Add("CUBZUSTPOS", OracleType.NVarChar).Value = dwData["CUBZUSTPOS"].ToString(); 
          or1.Parameters.Add("CUBLIEFDAT", OracleType.NVarChar).Value = dwData["CUBLIEFDAT"].ToString(); 
          or1.Parameters.Add("CUBAEDAT", OracleType.NVarChar).Value = dwData["CUBAEDAT"].ToString(); 
          or1.Parameters.Add("CUBFOLGE", OracleType.NVarChar).Value = dwData["CUBFOLGE"].ToString(); 
          or1.Parameters.Add("CUBLO", OracleType.NVarChar).Value = dwData["CUBLO"].ToString(); 
          or1.Parameters.Add("CUBLB", OracleType.NVarChar).Value = dwData["CUBLB"].ToString(); 
          or1.Parameters.Add("CUBLP", OracleType.NVarChar).Value = dwData["CUBLP"].ToString(); 
          or1.Parameters.Add("CUBAUFBKZ", OracleType.NVarChar).Value = dwData["CUBAUFBKZ"].ToString(); 
          or1.Parameters.Add("CUBSTKNR", OracleType.NVarChar).Value = dwData["CUBSTKNR"].ToString(); 
          or1.Parameters.Add("CUBANZCUT", OracleType.NVarChar).Value = dwData["CUBANZCUT"].ToString(); 
          or1.Parameters.Add("CUBSTATUS", OracleType.NVarChar).Value = dwData["CUBSTATUS"].ToString(); 
          or1.Parameters.Add("CUBSTPLNR", OracleType.NVarChar).Value = dwData["CUBSTPLNR"].ToString(); 
          or1.Parameters.Add("CUBSTPLGRP", OracleType.NVarChar).Value = dwData["CUBSTPLGRP"].ToString(); 
          or1.Parameters.Add("CUBFAZEIT", OracleType.NVarChar).Value = dwData["CUBFAZEIT"].ToString(); 
          or1.Parameters.Add("CUBART", OracleType.NVarChar).Value = dwData["CUBART"].ToString(); 
          or1.Parameters.Add("CUBSPERRE", OracleType.NVarChar).Value = dwData["CUBSPERRE"].ToString(); 
          or1.Parameters.Add("CUBNEUB", OracleType.NVarChar).Value = dwData["CUBNEUB"].ToString(); 
          or1.Parameters.Add("CUBNEUD", OracleType.NVarChar).Value = dwData["CUBNEUD"].ToString(); 
          or1.Parameters.Add("CUBAENB", OracleType.NVarChar).Value = dwData["CUBAENB"].ToString(); 
          or1.Parameters.Add("CUBAEND", OracleType.NVarChar).Value = dwData["CUBAEND"].ToString(); 
          or1.Parameters.Add("CUBAENZL", OracleType.NVarChar).Value = dwData["CUBAENZL"].ToString(); 
          or1.Parameters.Add("CUBFILLER", OracleType.NVarChar).Value = dwData["CUBFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
